package com.hsy.swu.home.leftdrawer

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hsy.swu.databinding.ItemCustomCourseBinding
import com.hsy.swu.home.course.CourseModel

class DelCustomCourseAdapter :
    RecyclerView.Adapter<DelCustomCourseAdapter.DelCustomCourseViewHolder>() {
    inner class DelCustomCourseViewHolder(private val binding: ItemCustomCourseBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(position: Int) {
            val customCourse = customCourseList[position]
            binding.apply {
                tvDel.setOnClickListener {
                    onDelClick?.invoke(position)
                }
                tvCourseName.text = customCourse.courseName
                tvSection.text = "${customCourse.section}节"
            }
        }
    }

    var customCourseList = emptyList<CourseModel>()

    private var onDelClick: ((Int) -> Unit)? = null

    fun setDelClickListener(listener: (Int) -> Unit) {
        onDelClick = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DelCustomCourseViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemCustomCourseBinding.inflate(inflater, parent, false)
        return DelCustomCourseViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return customCourseList.size
    }

    override fun onBindViewHolder(holder: DelCustomCourseViewHolder, position: Int) {
        holder.bind(position)
    }
}